import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * The type Add recipe name.
 *
 * @author Qiyin Huang
 * @date 24/06/10
 */
public class AddRecipeName extends JFrame implements ActionListener {
    /**
     * The Ask recipe name.
     */
    JLabel askRecipeName = new JLabel("Please enter the name of the recipe you added");
    /**
     * The Title input.
     */
    JTextField titleInput = new JTextField(20);
    /**
     * The Confirm button.
     */
    JButton confirmButton = new JButton("confirm");
    /**
     * The Error message.
     */
    JLabel errorMessage = new JLabel("");

    /**
     * Instantiates a new Add recipe name.
     */
    public AddRecipeName() {
        super("Enter recipe title");
        setSize(700, 200);
        addRecipeTitle();
        setLocationRelativeTo(null);
        this.setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * Add recipe title.
     */
    public void addRecipeTitle() {
        Container content = getContentPane();
        content.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        panel.add(askRecipeName);
        panel.add(titleInput);
        panel.add(confirmButton);
        content.add(panel, "North");

        errorMessage.setForeground(Color.RED);
        content.add(errorMessage, "Center");

        confirmButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == confirmButton) {
            String input = titleInput.getText().trim();
            if (input.isEmpty()) {
                errorMessage.setText("Please do not enter a blank name.");
            } else {
                AddRecipeFrame.newRecipe = new Recipe(input);
                this.setVisible(false);
                new AddRecipeDialog();
            }
        }
    }
}